<?php
namespace app\modules\mch\controllers\purchase;

use app\models\PostageRules;
use app\models\Banner;
use app\modules\mch\models\BannerForm;
use app\modules\mch\models\purchase\PurchaseSettingForm;
use app\models\PurchaseSetting;
use app\models\User;
use app\models\Goods;
use app\models\AttrGroup;
use app\models\Attr;
use yii\data\Pagination;
use app\models\Cat;

class DefaultController extends Controller
{
    //基础配置
    public function actionSetting(){
		
        $model = PurchaseSetting::findOne([
            'store_id' => $this->store->id, 
        ]);
        if(!$model) {
            $model = new PurchaseSetting();
        }
        if(\Yii::$app->request->isPost){
            $form = new PurchaseSettingForm();
            $form->attributes = \Yii::$app->request->post();
            $form->model = $model;
            $form->store_id = $this->store->id;
            return $form->save();
        }
		
		$catList = Cat::find()->asArray()->where(['store_id' => $this->store->id, 'is_delete' => 0])->all();
		foreach( $catList as $k => $v ) {
			$data = array( $v['name'] );
			$this->getparentall( $v['parent_id'] , $data );
			$data = array_reverse($data);
			$catList[$k]['name'] = implode( ' - ' , $data);
		}

        return $this->render('setting', [
            'setting' => $model,
			'cat_list' => $catList
        ]);
    } 
	
	private function getparentall( $cat_id , &$data ) {
		if( $cat_id > 0 ) {
			$find = Cat::findOne(['store_id' => $this->store->id, 'is_delete' => 0 , 'id' => $cat_id]);
			$data[] = $find->name;
			$this->getparentall( $find->parent_id , $data );
		}
	}

  
}